package ffhs.lernkartei.persistence;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class StacksDataSource {

	// Database fields
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;
	private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
			MySQLiteHelper.COLUMN_STACK };

	/**
	 * @param context
	 */
	public StacksDataSource(Context context) {
		dbHelper = new MySQLiteHelper(context);
	}

	/**
	 * @throws SQLException
	 */
	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	/**
	 * 
	 */
	public void close() {
		dbHelper.close();
	}

	/**
	 * Creates a new Stack entry in the database
	 * 
	 * @param stack
	 * @return
	 */
	public Stack createStack(String stack) {
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.COLUMN_STACK, stack);
		long insertId = database.insert(MySQLiteHelper.TABLE_STACKS, null,
				values);
		// To show how to query
		Cursor cursor = database.query(MySQLiteHelper.TABLE_STACKS, allColumns,
				MySQLiteHelper.COLUMN_ID + " = " + insertId, null, null, null,
				null);
		cursor.moveToFirst();
		return cursorToStack(cursor);
	}

	/**
	 * Deletes a Stack entry in the database
	 * 
	 * @param stack
	 */
	public void deleteStack(Stack stack) {
		long id = stack.getId();
		System.out.println("Stack deleted with id: " + id);
		database.delete(MySQLiteHelper.TABLE_STACKS, MySQLiteHelper.COLUMN_ID
				+ " = " + id, null);
	}

	/**
	 * Returns a list of all Stack entries in the database.
	 * 
	 * @return
	 */
	public List<Stack> getAllStacks() {
		List<Stack> stacks = new ArrayList<Stack>();
		Cursor cursor = database.query(MySQLiteHelper.TABLE_STACKS, allColumns,
				null, null, null, null, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Stack stack = cursorToStack(cursor);
			stacks.add(stack);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return stacks;
	}

	/**
	 * Returns the Stack entry to the corresponding stack name
	 * 
	 * @param stackname
	 * @return
	 */
	public Stack getStackByName(String stackname) {
		// Selection criterias
		String where = MySQLiteHelper.COLUMN_STACK + "=?";
		String[] args = { stackname };
		Cursor cursor = database.query(MySQLiteHelper.TABLE_STACKS, allColumns,
				where, args, null, null, null);
		cursor.moveToFirst();

		Stack stack = null;

		while (!cursor.isAfterLast()) {
			stack = cursorToStack(cursor);
			cursor.moveToNext();
		}

		// Make sure to close the cursor
		cursor.close();
		return stack;
	}

	/**
	 * @param cursor
	 * @return
	 */
	private Stack cursorToStack(Cursor cursor) {
		Stack stack = new Stack();
		stack.setId(cursor.getLong(0));
		stack.setStack(cursor.getString(1));
		return stack;
	}
}